<template>
  <div class="myContainer" >
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">                   
        <a id="downlink"></a>                 
        <el-button type="primary" class="myBgGreen" @click="onExport" :loading="loadingExport">导出</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">    	
      <el-col :span="3">
        <template>
          <el-select 
            v-model="hierarchyValue"
            @change="changeLevel"
            placeholder="层级">
            <el-option
              v-for="item in hierarchyList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="3">
        <template>
          <el-select
            v-model="regionValue"
            multiple
            collapse-tags
            class="mySelect"
            placeholder="区域">
            <el-option
              v-for="item in regionList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="3">
        <template>
          <el-select v-model="worklistType" clearable placeholder="全部工单类型">
            <el-option
              v-for="item in worklistOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="6">
        <el-date-picker
          v-model="time"
          type="daterange" 
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          unlink-panels
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-col> 
      <el-col :span="3" v-if="disabledNotTime">
        <template>
          <el-select v-model="notTime" clearable placeholder="周期段">
            <el-option
              v-for="item in notTimeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="3" v-if="disabledCycle">
        <el-input placeholder="请输入未完成周期" v-model="cycle"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="search">查 询</el-button>
      </el-col>
    </el-row>
   	<el-row class="myMarginTop_10px">
      <el-col :span="24">
        <template>
          <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="不及时" name="first">
              <el-table
                ref="multipleTable"
                :data="list"
                :border = true
                :stripe = true
                v-loading="loading"
                style="width: 100%">
                <el-table-column
                  type="index"
                  label="序号"
                  width="55">
                </el-table-column>
                <!-- <el-table-column
                  prop="custArea"
                  label="片区名称">
                </el-table-column> -->
                <el-table-column
                  prop="custCorpName"
                  label="项目名称">
                </el-table-column>
                <!-- <el-table-column
                  prop="area_name"
                  label="工单类型">
                </el-table-column> -->
                <el-table-column
                  prop="_woType"
                  label="类别">
                </el-table-column>
                <el-table-column
                  prop="inTimeSum"
                  label="及时数量">
                </el-table-column>
                <el-table-column
                  prop="notTimeSum"
                  label="不及时数量">
                </el-table-column>
                <el-table-column
                  prop="notTimeRate"
                  label="不及时占比">
                </el-table-column>
                <el-table-column
                  label="不及时周期">
                  <template scope="scope">
                    <div v-for="(key,value) in scope.row.notTimes">
                      {{value}} : {{key}}
                    </div>
                  </template>
                </el-table-column>  
                <el-table-column
                  label="功能操作"
                  width="120">
                  <template scope="scope">
                    <el-button class="look" type="text" size="small" v-if="!scope.row.notTimeSum==0" @click="getInfo(1,scope.row.notTimeList)">查看</el-button> 
                    <span v-else>-</span> 
                  </template>
                </el-table-column>
              </el-table>

              <div class="block">                    
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="page"
                  :page-sizes="[10, 15]"
                  :page-size="limit"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
              </div>
            </el-tab-pane>
            <el-tab-pane label="未完成" name="second">
              <el-table
                ref="multipleTable"
                :data="list"
                :border = true
                :stripe = true
                v-loading="loading"
                style="width: 100%">
                <el-table-column
                  type="index"
                  label="序号"
                  width="55">
                </el-table-column>
                <!-- <el-table-column
                  prop="custArea"
                  label="片区名称">
                </el-table-column> -->
                <el-table-column
                  prop="custCorpName"
                  label="项目名称">
                </el-table-column>
                <!-- <el-table-column
                  prop="area_name"
                  label="工单类型">
                </el-table-column> -->
                <el-table-column
                  prop="_woType"
                  label="类别">
                </el-table-column>
                <el-table-column
                  prop="completeSum"
                  label="已完成数量">
                </el-table-column>
                <el-table-column
                  prop="notSum"
                  label="未完成数量">
                </el-table-column>
                <el-table-column
                  prop="notRate"
                  label="未完成占比">
                </el-table-column>
                <el-table-column
                  label="未完成周期">
                  <template scope="scope">
                    <div v-for="(key,value) in scope.row.notTime">
                      {{value}} : {{key}}
                    </div>
                  </template>
                </el-table-column>  
                <el-table-column
                  label="功能操作"
                  width="120">
                  <template scope="scope">
                    <el-button class="look" type="text" size="small" v-if="!scope.row.notSum==0" @click="getInfo(2,scope.row.notList)">查看</el-button> 
                    <span v-else>-</span> 
                  </template>
                </el-table-column>
              </el-table>

              <div class="block">                    
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="page"
                  :page-sizes="[10, 15]"
                  :page-size="limit"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
              </div>
            </el-tab-pane>
            <el-tab-pane label="不满意" name="third">
              <el-table
                ref="multipleTable"
                :data="list"
                :border = true
                :stripe = true
                v-loading="loading"
                style="width: 100%">
                <el-table-column
                  type="index"
                  label="序号"
                  width="55">
                </el-table-column>
                <!-- <el-table-column
                  prop="custArea"
                  label="片区名称">
                </el-table-column> -->
                <el-table-column
                  prop="custCorpName"
                  label="项目名称">
                </el-table-column>
                <!-- <el-table-column
                  prop="area_name"
                  label="工单类型">
                </el-table-column> -->
                <el-table-column
                  prop="_woType"
                  label="类别">
                </el-table-column>
                <el-table-column
                  prop="general"
                  label="中评(占比)">
                </el-table-column>
                <el-table-column
                  prop="good"
                  label="好评(占比)">
                </el-table-column>
                <el-table-column
                  prop="poor"
                  label="差评(占比)">
                </el-table-column>
                <!-- <el-table-column
                  prop="area_name"
                  label="中评占比">
                </el-table-column>  
                <el-table-column
                  prop="area_name"
                  label="好评占比">
                </el-table-column> 
                <el-table-column
                  prop="area_name"
                  label="差评占比">
                </el-table-column>  -->
                <el-table-column
                  label="功能操作"
                  width="120">
                  <template scope="scope">
                    <el-button class="look" type="text" size="small" v-if="!scope.row.poorList=='0 (0.00%)'" @click="getInfo(3,scope.row.poorList)">查看</el-button>
                    <span v-else>-</span> 
                  </template>
                </el-table-column>
              </el-table>

              <div class="block">                    
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="page"
                  :page-sizes="[10, 15]"
                  :page-size="limit"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
              </div>
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-col>
    </el-row>
    <!-- 不及时明细 START -->
    <el-dialog
      title="不及时明细表"
      :visible.sync="dialogVisible_1"
      width="50%">
      <el-row :gutter="10">      
        <el-col :span="24">
          <el-table
            ref="multipleTable"
            :data="dialoglist"
            :border = true
            :stripe = true
            style="width: 100%">
            <el-table-column
              type="index"
              label="序号"
              width="55">
            </el-table-column>
            <el-table-column
              prop="woOther1"
              label="工单单号">
            </el-table-column>
            <el-table-column
              prop="woOther16"
              label="责任人">
            </el-table-column>
            <el-table-column
              prop="woCreateTime"
              label="开始时间">
              <template scope="scope">
                {{GMTToStr2(scope.row.woCreateTime)}}
              </template>
            </el-table-column>
            <el-table-column
              prop="woEndTime"
              label="结束时间">
              <template scope="scope">
                {{GMTToStr2(scope.row.woEndTime)}}
              </template>
            </el-table-column>
            <el-table-column
              width="200"
              label="处理时长">
              <template scope="scope">
                {{formatDuring(scope.row.woEndTime-scope.row.woCreateTime)}}
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible_1 = false">关 闭</el-button>
        <el-button type="primary" class="myBgGreen" @click="onExport2" :loading="loadingExport2">导出</el-button>
      </span>
    </el-dialog>
    <!-- 不及时明细 END -->
    <!-- 未完成明细 START -->
    <el-dialog
      title="未完成明细表"
      :visible.sync="dialogVisible_2"
      width="50%">
      <el-row :gutter="10">       
        <el-col :span="24">
          <el-table
            ref="multipleTable"
            :data="dialoglist"
            :border = true
            :stripe = true
            style="width: 100%">
            <el-table-column
              type="index"
              label="序号"
              width="55">
            </el-table-column>
            <el-table-column
              prop="woOther1"
              label="工单单号">
            </el-table-column>
            <el-table-column
              prop="woOther16"
              label="责任人">
            </el-table-column>
            <el-table-column
              prop="woCreateTime"
              label="开始时间">
              <template scope="scope">
                {{GMTToStr2(scope.row.woCreateTime)}}
              </template>
            </el-table-column>
            <el-table-column
              label="未完成周期">
              <template scope="scope">
                {{formatDuring2(scope.row.woCreateTime)}}
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible_2 = false">关 闭</el-button>
        <el-button type="primary" class="myBgGreen" @click="onExport2" :loading="loadingExport2">导出</el-button>
      </span>
    </el-dialog>
    <!-- 未完成明细 END -->
    <!-- 评价明细 START -->
    <el-dialog
      title="差评明细表"
      :visible.sync="dialogVisible_3"
      width="50%">
      <el-row :gutter="10">      
        <el-col :span="24">
          <el-table
            ref="multipleTable"
            :data="dialoglist"
            :border = true
            :stripe = true
            style="width: 100%">
            <el-table-column
              type="index"
              label="序号"
              width="55">
            </el-table-column>
            <el-table-column
              prop="woOther1"
              width="150"
              label="工单单号">
            </el-table-column>
            <!-- <el-table-column
              prop="woStep4Status"
              label="评价等级">
            </el-table-column> -->
            <el-table-column
              prop="woOther16"
              width="100"
              label="责任人">
            </el-table-column>
            <el-table-column
              prop="woStep3Answer"
              label="评价文本">
            </el-table-column>
            <el-table-column
              prop="woOther18"
              width="100"
              label="评价渠道">
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible_3 = false">关 闭</el-button>
        <el-button type="primary" class="myBgGreen" @click="onExport2" :loading="loadingExport2">导出</el-button>
      </span>
    </el-dialog>
    <!-- 评价明细 END -->
  </div>
</template>
<style>
  .mySelect .el-input__inner,.mySelect .el-select__tags{
    height: 40px !important;
    overflow: hidden;
  }
  .mySelect.el-select .el-tag{
    margin: 8px 0 3px 6px !important;
  }
</style>
<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myPaddingBottom_10px{
    padding-bottom: 10px;
  }
   .myMarginTop_5px{
    margin-top: 5px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
    padding-right: 10px;
  }
</style>

<script>
  import { keyMapping , getRegioninfo } from '@/api/affair_setting';
  import { EXTENSION , PASSWORD , BASE_400 } from '@/config/baseUrl';
  import { getLevelArea } from "@/api/area";
  let XLSX = require('xlsx');
  
  export default {
    data(){
      return{  
        title:"工单事件统计表",
        limit: 10,  
        page: 1,  
        total:null,  
        hierarchyValue:1,
        hierarchyList:[
          {value:1,label:"集团"},
          {value:2,label:"片区"},
          {value:3,label:"小区"},
        ],
        regionValue:[],
        regionList:[],
        regionOptions: [],
        regionOptSelected: [],
        worklistType:"",
        worklistOptions:[
          {label:"报修类",value:"报修类"},
          {label:"报事类",value:"报事类"},
          {label:"投诉类",value:"投诉类"}
        ],
        time:[],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一年',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
              picker.$emit('pick', [start, end]);
            }
          }]
        }, 
        activeName:"first",
        list:[],
        dialogVisible_1:false,
        dialogVisible_2:false,
        dialogVisible_3:false,
        tj:"",
        tjOptions:[
          { label : ">" , value : ">" },
          { label : ">=" , value : ">=" },
          { label : "<" , value : "<" },
          { label : "<=" , value : "<=" },
          { label : "=" , value : "=" },
          { label : "!=" , value : "!=" }
        ],
        gx:"",
        gxOptions:[
          { label : "并且" , value : "并且" },
          { label : "或者" , value : "或者" },
        ],
        i:0,
        loading:false,
        type:2,
        dialoglist:[],
        loadingExport:false,
        loadingExport2:false,
        notTime:"",
        notTimeOptions:[
          { label:"0分钟-15分钟", value:"0,15"},
          { label:"15分钟-30分钟", value:"15,30"},
          { label:"30分钟-45分钟", value:"30,45"},
          { label:"45分钟-1小时", value:"45,60"},
          { label:"1小时-1小时15分钟", value:"60,75"},
          { label:"1小时15分钟-1小时30分钟", value:"75,90"},
          { label:"1小时30分钟-1小时45分钟", value:"90,105"},
          { label:"1小时45分钟-2小时", value:"105,120"},
          { label:"2小时-2小时15分钟", value:"120,135"},
          { label:"2小时15分钟2小时30分钟", value:"135,150"},
          { label:"2小时30分钟-2小时45分钟", value:"150,165"},
          { label:"2小时45分钟-3小时", value:"165,180"},
          { label:"3小时-3小时15分钟", value:"180,195"},
          { label:"3小时15分钟-3小时30分钟", value:"195,210"},
          { label:"3小时30分钟-3小时45分钟", value:"210,225"},
          { label:"3小时45分钟-4小时", value:"225,240"},
          { label:"4小时-4小时15分钟", value:"240,255"},
          { label:"4小时15分钟-4小时30分钟", value:"255,270"},
          { label:"4小时30分钟-4小时45分钟", value:"270,285"},
          { label:"4小时45分钟-5小时", value:"285,300"},
          { label:"5小时-5小时15分钟", value:"300,315"},
          { label:"5小时15分钟-5小时30分钟", value:"315,330"},
          { label:"5小时30分钟-5小时45分钟", value:"330,345"},
          { label:"5小时45分钟-6小时", value:"345,360"},
          { label:"6小时以上", value:"360,"},
        ],
        disabledNotTime:true,
        disabledCycle:false,
        cycle:""
      }
    },
    filters: {
       
    },
    computed: {
      
    },
    created(){
      this.time=[this.GMTToStr(new Date(new Date().getTime() - 3600 * 1000 * 24 * 7)),this.GMTToStr(new Date())];
    },
    mounted(){
      document.title = this.title;
      this.changeLevel(1);
      this.outFile = document.getElementById('downlink');
    },
    methods:{
      handleSizeChange(val) {
        this.limit = val;
        if(this.type==2){
          this.ongetWorkTotal();
        }else if(this.type==3){
          this.ongetWorkTotal3();
        }else if(this.type==4){
          this.ongetWorkTotal4();
        }
      },
      handleCurrentChange(val) {
        this.page = val;
        if(this.type==2){
          this.ongetWorkTotal();
        }else if(this.type==3){
          this.ongetWorkTotal3();
        }else if(this.type==4){
          this.ongetWorkTotal4();
        }
      },
      handleClick(tab, event) {
        this.page=1;
        this.limit=10;
        if(tab.name=='first'){
          this.type=2;
          this.disabledNotTime=true;
          this.disabledCycle=false;
          this.ongetWorkTotal();
        }else if(tab.name=='second'){
          this.type=3;
          this.disabledNotTime=false;
          this.disabledCycle=true;
          this.ongetWorkTotal3();
        }else if(tab.name=='third'){
          this.type=4;
          this.disabledNotTime=false;
          this.disabledCycle=false;
          this.ongetWorkTotal4();
        }
      },
      GMTToStr(time){
        let date = new Date(time)
        let Str=date.getFullYear() + '-' +
        (date.getMonth() + 1) + '-' + 
        date.getDate() 
        return Str
      },
      GMTToStr2(time){
        let date = new Date(time)
        let Str=date.getFullYear() + '-' +
        (date.getMonth() + 1) + '-' + 
        date.getDate() 
        + ' ' + 
        date.getHours() + ':' + 
        date.getMinutes() + ':' + 
        date.getSeconds()
        return Str
      },
      formatDuring(mss) {
        let days = parseInt(mss / (1000 * 60 * 60 * 24));
        let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
        let seconds = (mss % (1000 * 60)) / 1000;
        return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 ";
        // let minutes = Math.ceil(mss / (1000 * 60));
        // return minutes+' 分钟';
      }, 
      formatDuring2(createTime){
        let now=(new Date()).getTime();
        let days = Math.floor((now-createTime) / (1000 * 60 * 60 * 24));
        return days+' 天';
      },
      search(){
        let vm=this;
        let params={
          level:vm.hierarchyValue,
          id_list:vm.regionValue.toString()
        }
        getRegioninfo(params).then(response => {
          let res=response.data;
          if(res){
            vm.custCorpName=res.replace(/&/g,'%26');
            if(vm.type==2){
              vm.ongetWorkTotal();
            }else if(vm.type==3){
              vm.ongetWorkTotal3();
            }else if(vm.type==4){
              vm.ongetWorkTotal4();
            }
          }
        }).catch(error => {})
      },
      ongetWorkTotal(){
        let vm=this;
        vm.list=[];
        vm.loading=true;
        let startTime=vm.notTime.split(',')[0]||'';
        let endTime=vm.notTime.split(',')[1]||'';
        vm.$http.get(`${BASE_400}/voip/ext/op/api/getWorkTotal.json?type=${vm.type}&extension=${EXTENSION}&password=${PASSWORD}&startTime=${vm.time[0]} 00:00:00&endTime=${vm.time[1]} 23:59:59&wotName=${vm.worklistType}&custCorpName=${vm.custCorpName}&notStartTime=${startTime}&notEndTime=${endTime}&page=${vm.page}&limit=${vm.limit}&orderField=custCorpName`).then(response => {
          let res=response.data;
          vm.list=res.rows||[];
          vm.limit=res.limit;
          vm.page=res.page;
          vm.total=res.total;
          vm.loading=false;
        }).catch(error => {
          vm.loading=false;
          vm.$message.error("执行失败！");
        })
      },
      ongetWorkTotal3(){
        let vm=this;
        vm.list=[];
        vm.loading=true;
        vm.$http.get(`${BASE_400}/voip/ext/op/api/getWorkTotal.json?type=${vm.type}&extension=${EXTENSION}&password=${PASSWORD}&startTime=${vm.time[0]} 00:00:00&endTime=${vm.time[1]} 23:59:59&wotName=${vm.worklistType}&custCorpName=${vm.custCorpName}&page=${vm.page}&limit=${vm.limit}&findDay=${vm.cycle}&orderField=custCorpName`).then(response => {
          let res=response.data;
          vm.list=res.rows||[];
          vm.limit=res.limit;
          vm.page=res.page;
          vm.total=res.total;
          vm.loading=false;
        }).catch(error => {
          vm.loading=false;
          vm.$message.error("执行失败！");
        })
      },
      ongetWorkTotal4(){
        let vm=this;
        vm.list=[];
        vm.loading=true;
        vm.$http.get(`${BASE_400}/voip/ext/op/api/getWorkTotal.json?type=${vm.type}&extension=${EXTENSION}&password=${PASSWORD}&startTime=${vm.time[0]} 00:00:00&endTime=${vm.time[1]} 23:59:59&wotName=${vm.worklistType}&custCorpName=${vm.custCorpName}&page=${vm.page}&limit=${vm.limit}&orderField=custCorpName`).then(response => {
          let res=response.data;
          vm.list=res.rows||[];
          vm.limit=res.limit;
          vm.page=res.page;
          vm.total=res.total;
          vm.loading=false;
        }).catch(error => {
          vm.loading=false;
          vm.$message.error("执行失败！");
        })
      },
      changeLevel(val){
        let vm=this;
        let params={
          level:val
        }
        getLevelArea(params).then(response => {
          let res=response.data;
          vm.regionList=res;
          if(res.length>0 && vm.hierarchyValue==1){
            vm.regionValue=[res[0].value];
            if(vm.i==0){
              vm.search();
              vm.i=1;
            }
          }else{
            vm.regionValue=[];
          }
        }).catch(error => {})
      },
      onExport(){
        let vm=this;
        vm.loadingExport=true;
        // let res=[];
        if(vm.type==2){
          let startTime=vm.notTime.split(',')[0]||'';
          let endTime=vm.notTime.split(',')[1]||'';
          vm.$http.get(`${BASE_400}/voip/ext/op/api/getWorkTotal.json?type=${vm.type}&extension=${EXTENSION}&password=${PASSWORD}&startTime=${vm.time[0]} 00:00:00&endTime=${vm.time[1]} 23:59:59&wotName=${vm.worklistType}&custCorpName=${vm.custCorpName}&notStartTime=${startTime}&notEndTime=${endTime}&page=1&limit=${vm.total}&orderField=custCorpName`).then(response => {
            let res=response.data.rows||[];
            vm.toexport(res);
          }).catch(error => {
            vm.$message.error("执行失败！");
          })
        }else if(vm.type==3){
          vm.$http.get(`${BASE_400}/voip/ext/op/api/getWorkTotal.json?type=${vm.type}&extension=${EXTENSION}&password=${PASSWORD}&startTime=${vm.time[0]} 00:00:00&endTime=${vm.time[1]} 23:59:59&wotName=${vm.worklistType}&custCorpName=${vm.custCorpName}&page=1&limit=${vm.total}&findDay=${vm.cycle}&orderField=custCorpName`).then(response => {
            let res=response.data.rows||[];
            vm.toexport(res);
          }).catch(error => {
            vm.$message.error("执行失败！");
          })
        }else if(vm.type==4){
          vm.$http.get(`${BASE_400}/voip/ext/op/api/getWorkTotal.json?type=${vm.type}&extension=${EXTENSION}&password=${PASSWORD}&startTime=${vm.time[0]} 00:00:00&endTime=${vm.time[1]} 23:59:59&wotName=${vm.worklistType}&custCorpName=${vm.custCorpName}&page=1&limit=${vm.total}&orderField=custCorpName`).then(response => {
            let res=response.data.rows||[];
            vm.toexport(res);
          }).catch(error => {
            vm.$message.error("执行失败！");
          })
        }
      },
      toexport(res){
        let vm=this;
        let name="";
        let data = [{}];
        if(res.length==0){
          vm.loadingExport=false;
          vm.$message.info("没有可导出的数据！");
          return;
        }
        let key=[];
        let subRes=[];
        let subObj={};
        if(vm.type==2){
          key=['custCorpName','_woType','_sum','time','inTimeSum','notTimeRate','notTimeSum'];
          for(let i in res){
            subObj={};
            for(let j in res[i]){
              if(key.indexOf(j)!=-1){
                subObj[j]=res[i][j];
              }
            }
            subRes.push(subObj);
          }
          name="工单事件不及时统计表"+vm.time[0]+'至'+vm.time[1];
        }else if(vm.type==3){
          key=['custCorpName','_woType','_sum','completeSum','time','notSum','notRate'];
          for(let i in res){
            subObj={};
            for(let j in res[i]){
              if(key.indexOf(j)!=-1){
                subObj[j]=res[i][j];
              }
            }
            subRes.push(subObj);
          }
          name="工单事件未完成统计表"+vm.time[0]+'至'+vm.time[1];
        }else if(vm.type==4){
          key=['custCorpName','_woType','_sum','general','time','good','poor'];
          for(let i in res){
            subObj={};
            for(let j in res[i]){
              if(key.indexOf(j)!=-1){
                subObj[j]=res[i][j];
              }
            }
            subRes.push(subObj);
          }
          name="工单事件不满意统计表"+vm.time[0]+'至'+vm.time[1];
        }
        for (let k in subRes[0]) {
          data[0][k] = k
        }
        data = data.concat(subRes);
        vm.downloadExl(data, name);
        vm.loadingExport=false;
        vm.$message.success(`导出成功！总共${subRes.length}条数据。`);
      },
      onExport2(){
        let vm=this;
        vm.loadingExport2=true;
        let res=vm.dialoglist;
        let name="";
        let data = [{}];
        if(res.length==0){
          vm.loadingExport2=false;
          vm.$message.info("没有可导出的数据！");
          return;
        }
        let key=[];
        let subRes=[];
        let subObj={};
        if(vm.type==2){
          key=['woOther1','woOther16','woCreateTime','woEndTime','timeConsuming'];
          for(let i in res){
            res[i]['timeConsuming']=vm.formatDuring(res[i]['woEndTime']-res[i]['woCreateTime']);
            subObj={};
            for(let j in res[i]){
              if(key.indexOf(j)!=-1){
                if(j=='woCreateTime'){
                  subObj[j]=vm.GMTToStr2(res[i][j]);
                }else if(j=='woEndTime'){
                  subObj[j]=vm.GMTToStr2(res[i][j]);
                }else{
                  subObj[j]=res[i][j];
                }
              }
            }
            subRes.push(subObj);
          }
          name="工单事件不及时明细统计表"+vm.time[0]+'至'+vm.time[1];
        }else if(vm.type==3){
          key=['woOther1','woOther16','woCreateTime','unfinishedCycle'];
          for(let i in res){
            res[i]['unfinishedCycle']=vm.formatDuring2(res[i]['woCreateTime']);
            subObj={};
            for(let j in res[i]){
              if(key.indexOf(j)!=-1){
                if(j=='woCreateTime'){
                  subObj[j]=vm.GMTToStr2(res[i][j]);
                }else{
                  subObj[j]=res[i][j];
                }
              }
            }
            subRes.push(subObj);
          }
          name="工单事件未完成明细统计表"+vm.time[0]+'至'+vm.time[1];
        }else if(vm.type==4){
          key=['woOther1','woOther16','woStep3Answer'];
          for(let i in res){
            subObj={};
            for(let j in res[i]){
              if(key.indexOf(j)!=-1){
                subObj[j]=res[i][j];
              } 
            }
            subRes.push(subObj);
          }
          name="工单事件不满意明细统计表"+vm.time[0]+'至'+vm.time[1];
        }
        for (let k in subRes[0]) {
          data[0][k] = k
        }
        data = data.concat(subRes);
        vm.downloadExl(data, name);
        vm.loadingExport2=false;
        vm.$message.success(`导出成功！总共${subRes.length}条数据。`);
      },
      downloadExl: function (json, downName, type) {  // 导出到excel
        let keyMap = []
        for (let k in json[0]) {
          keyMap.push(k);
        }
        let tmpdata = [] 
        json.map((v, i) => keyMap.map((k, j) => Object.assign({}, {
          v: v[k],
          position: (j > 25 ? this.getCharCol(j) : String.fromCharCode(65 + j)) + (i + 1)
        }))).reduce((prev, next) => prev.concat(next)).forEach(function (v) {
          tmpdata[v.position] = {
            v: keyMapping[v.v] || v.v
          }
        })
        let outputPos = Object.keys(tmpdata)  
        let tmpWB = {
          SheetNames: ['工单事件统计表'], 
          Sheets: {
            '工单事件统计表': Object.assign({},
              tmpdata, 
              {
                '!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] 
              })
          }
        }
        let tmpDown = new Blob([this.s2ab(XLSX.write(tmpWB,
          {bookType: (type === undefined ? 'xlsx' : type), bookSST: false, type: 'binary'} 
        ))], {
          type: ''
        }) 
        var href = URL.createObjectURL(tmpDown)  
        this.outFile.download = downName + '.xlsx'  
        this.outFile.href = href  
        this.outFile.click()  

        setTimeout(function () {  
          URL.revokeObjectURL(tmpDown) 
        }, 100)
      },
      s2ab: function (s) { 
        var buf = new ArrayBuffer(s.length)
        var view = new Uint8Array(buf)
        for (var i = 0; i !== s.length; ++i) {
          view[i] = s.charCodeAt(i) & 0xFF
        }
        return buf
      },
      getCharCol: function (n) { 
        let s = ''
        let m = 0
        while (n > 0) {
          m = n % 26 + 1
          s = String.fromCharCode(m + 64) + s
          n = (n - m) / 26
        }
        return s
      },
      getInfo(item,val){
        let vm=this;
        vm.dialoglist=val;
        vm["dialogVisible_"+item]=true;
      }
     
    }

  }


</script>

